﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ORMAC.UI.WPF.ViewModel;
using ORMAC.Paterns;
using ORMAC.Attributes;
using System.IO;
using System.Xml.Serialization;
using ORMAC.UI.WPF.Properties;

namespace ORMAC.UI.WPF.Business
{
    /// <summary>
    /// class to contain all configuration information for ormac
    /// </summary>
    [Table("ORMACConfiguration")]
    public class ORMACConfiguration:Singleton<ORMACConfiguration>
    {
        private List<ORMACDBConfiguration> dBConfiguration=new List<ORMACDBConfiguration>();
        [Table("Databases")]
        public List<ORMACDBConfiguration> DBConfiguration
        {
            get
            {
                return dBConfiguration;
            }
            set
            {
                dBConfiguration = value;
            }
        }

        internal string CreateConfigFile()
        {
            string fileName = System.IO.Path.GetTempPath() + Settings.Default.ConfigurationFileName;
            using (FileStream fstream = new FileStream(fileName, FileMode.Create))
            {
                XmlSerializer ser = new XmlSerializer(this.DBConfiguration.GetType());
                ser.Serialize(fstream, DBConfiguration);
                fstream.Close();
            }
            return fileName;
        }
        internal void LoadConfigFile(string configFilePath)
        {
            using (FileStream fstream = new FileStream(configFilePath, FileMode.Open))
            {
                XmlSerializer ser = new XmlSerializer(this.GetType());
                DBConfiguration=(List<ORMACDBConfiguration>) ser.Deserialize(fstream);
                fstream.Close();
            }
            
        }
    }
}
